home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / atlconv.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  8.0 KB  |  222 lines

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-1997 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Active Template Library Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Active Template Library product.
  10.  
  11. #ifndef __ATLCONV_H__
  12. #define __ATLCONV_H__
  13.  
  14. #ifndef __cplusplus
  15.     #error ATL requires C++ compilation (use a .cpp suffix)
  16. #endif
  17.  
  18. #ifndef _INC_MALLOC
  19. #include <malloc.h>
  20. #endif // _INC_MALLOC
  21.  
  22. #pragma pack(push,8)
  23.  
  24. // Make sure MFC's afxconv.h hasn't already been loaded to do this
  25. #ifndef USES_CONVERSION
  26.  
  27. #if !defined (_DEBUG)
  28. #define USES_CONVERSION int _convert; &_convert
  29. #else
  30. #define USES_CONVERSION int _convert = 0
  31. #endif
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // Global UNICODE<>ANSI translation helpers
  35. LPWSTR WINAPI AtlA2WHelper(LPWSTR lpw, LPCSTR lpa, int nChars);
  36. LPSTR WINAPI AtlW2AHelper(LPSTR lpa, LPCWSTR lpw, int nChars);
  37.  
  38. #ifndef ATLA2WHELPER
  39. #define ATLA2WHELPER AtlA2WHelper
  40. #define ATLW2AHELPER AtlW2AHelper
  41. #endif
  42.  
  43. #define A2W(lpa) (\
  44.     ((LPCSTR)lpa == NULL) ? NULL : (\
  45.         _convert = (lstrlenA(lpa)+1),\
  46.         ATLA2WHELPER((LPWSTR) alloca(_convert*2), lpa, _convert)))
  47.  
  48. #define W2A(lpw) (\
  49.     ((LPCWSTR)lpw == NULL) ? NULL : (\
  50.         _convert = (lstrlenW(lpw)+1)*2,\
  51.         ATLW2AHELPER((LPSTR) alloca(_convert), lpw, _convert)))
  52.  
  53. #define A2CW(lpa) ((LPCWSTR)A2W(lpa))
  54. #define W2CA(lpw) ((LPCSTR)W2A(lpw))
  55.  
  56. #if defined(_UNICODE)
  57. // in these cases the default (TCHAR) is the same as OLECHAR
  58.     inline size_t ocslen(LPCOLESTR x) { return lstrlenW(x); }
  59.     inline OLECHAR* ocscpy(LPOLESTR dest, LPCOLESTR src) { return lstrcpyW(dest, src); }
  60.     inline LPCOLESTR T2COLE(LPCTSTR lp) { return lp; }
  61.     inline LPCTSTR OLE2CT(LPCOLESTR lp) { return lp; }
  62.     inline LPOLESTR T2OLE(LPTSTR lp) { return lp; }
  63.     inline LPTSTR OLE2T(LPOLESTR lp) { return lp; }
  64. #elif defined(OLE2ANSI)
  65. // in these cases the default (TCHAR) is the same as OLECHAR
  66.     inline size_t ocslen(LPCOLESTR x) { return lstrlen(x); }
  67.     inline OLECHAR* ocscpy(LPOLESTR dest, LPCOLESTR src) { return lstrcpy(dest, src); }
  68.     inline LPCOLESTR T2COLE(LPCTSTR lp) { return lp; }
  69.     inline LPCTSTR OLE2CT(LPCOLESTR lp) { return lp; }
  70.     inline LPOLESTR T2OLE(LPTSTR lp) { return lp; }
  71.     inline LPTSTR OLE2T(LPOLESTR lp) { return lp; }
  72. #else
  73.     inline size_t ocslen(LPCOLESTR x) { return lstrlenW(x); }
  74.     //lstrcpyW doesn't work on Win95, so we do this
  75.     inline OLECHAR* ocscpy(LPOLESTR dest, LPCOLESTR src)
  76.     {return (LPOLESTR) memcpy(dest, src, (lstrlenW(src)+1)*sizeof(WCHAR));}
  77.     //CharNextW doesn't work on Win95 so we use this
  78.     #define T2COLE(lpa) A2CW(lpa)
  79.     #define T2OLE(lpa) A2W(lpa)
  80.     #define OLE2CT(lpo) W2CA(lpo)
  81.     #define OLE2T(lpo) W2A(lpo)
  82. #endif
  83.  
  84. #ifdef OLE2ANSI
  85.     inline LPOLESTR A2OLE(LPSTR lp) { return lp;}
  86.     inline LPSTR OLE2A(LPOLESTR lp) { return lp;}
  87.     #define W2OLE W2A
  88.     #define OLE2W A2W
  89.     inline LPCOLESTR A2COLE(LPCSTR lp) { return lp;}
  90.     inline LPCSTR OLE2CA(LPCOLESTR lp) { return lp;}
  91.     #define W2COLE W2CA
  92.     #define OLE2CW A2CW
  93. #else
  94.     inline LPOLESTR W2OLE(LPWSTR lp) { return lp; }
  95.     inline LPWSTR OLE2W(LPOLESTR lp) { return lp; }
  96.     #define A2OLE A2W
  97.     #define OLE2A W2A
  98.     inline LPCOLESTR W2COLE(LPCWSTR lp) { return lp; }
  99.     inline LPCWSTR OLE2CW(LPCOLESTR lp) { return lp; }
  100.     #define A2COLE A2CW
  101.     #define OLE2CA W2CA
  102. #endif
  103.  
  104. #ifdef _UNICODE
  105.     #define T2A W2A
  106.     #define A2T A2W
  107.     inline LPWSTR T2W(LPTSTR lp) { return lp; }
  108.     inline LPTSTR W2T(LPWSTR lp) { return lp; }
  109.     #define T2CA W2CA
  110.     #define A2CT A2CW
  111.     inline LPCWSTR T2CW(LPCTSTR lp) { return lp; }
  112.     inline LPCTSTR W2CT(LPCWSTR lp) { return lp; }
  113. #else
  114.     #define T2W A2W
  115.     #define W2T W2A
  116.     inline LPSTR T2A(LPTSTR lp) { return lp; }
  117.     inline LPTSTR A2T(LPSTR lp) { return lp; }
  118.     #define T2CW A2CW
  119.     #define W2CT W2CA
  120.     inline LPCSTR T2CA(LPCTSTR lp) { return lp; }
  121.     inline LPCTSTR A2CT(LPCSTR lp) { return lp; }
  122. #endif
  123.  
  124. inline BSTR OLE2BSTR(LPCOLESTR lp) {return ::SysAllocString(lp);}
  125. #if defined(_UNICODE)
  126. // in these cases the default (TCHAR) is the same as OLECHAR
  127.     inline BSTR T2BSTR(LPCTSTR lp) {return ::SysAllocString(lp);}
  128.     inline BSTR A2BSTR(LPCSTR lp) {USES_CONVERSION; return ::SysAllocString(A2COLE(lp));}
  129.     inline BSTR W2BSTR(LPCWSTR lp) {return ::SysAllocString(lp);}
  130. #elif defined(OLE2ANSI)
  131. // in these cases the default (TCHAR) is the same as OLECHAR
  132.     inline BSTR T2BSTR(LPCTSTR lp) {return ::SysAllocString(lp);}
  133.     inline BSTR A2BSTR(LPCSTR lp) {return ::SysAllocString(lp);}
  134.     inline BSTR W2BSTR(LPCWSTR lp) {USES_CONVERSION; return ::SysAllocString(W2COLE(lp));}
  135. #else
  136.     inline BSTR T2BSTR(LPCTSTR lp) {USES_CONVERSION; return ::SysAllocString(T2COLE(lp));}
  137.     inline BSTR A2BSTR(LPCSTR lp) {USES_CONVERSION; return ::SysAllocString(A2COLE(lp));}
  138.     inline BSTR W2BSTR(LPCWSTR lp) {return ::SysAllocString(lp);}
  139. #endif
  140.  
  141. #ifdef _WINGDI_
  142. /////////////////////////////////////////////////////////////////////////////
  143. // Global UNICODE<>ANSI translation helpers
  144. LPDEVMODEW AtlDevModeA2W(LPDEVMODEW lpDevModeW, LPDEVMODEA lpDevModeA);
  145. LPDEVMODEA AtlDevModeW2A(LPDEVMODEA lpDevModeA, LPDEVMODEW lpDevModeW);
  146. LPTEXTMETRICW AtlTextMetricA2W(LPTEXTMETRICW lptmW, LPTEXTMETRICA pltmA);
  147. LPTEXTMETRICA AtlTextMetricW2A(LPTEXTMETRICA lptmA, LPTEXTMETRICW pltmW);
  148.  
  149. #ifndef ATLDEVMODEA2W
  150. #define ATLDEVMODEA2W AtlDevModeA2W
  151. #define ATLDEVMODEW2A AtlDevModeW2A
  152. #define ATLTEXTMETRICA2W AtlTextMetricA2W
  153. #define ATLTEXTMETRICW2A AtlTextMetricW2A
  154. #endif
  155.  
  156. #define DEVMODEW2A(lpw)\
  157.     ((lpw == NULL) ? NULL : ATLDEVMODEW2A((LPDEVMODEA)alloca(sizeof(DEVMODEA)+lpw->dmDriverExtra), lpw))
  158. #define DEVMODEA2W(lpa)\
  159.     ((lpa == NULL) ? NULL : ATLDEVMODEA2W((LPDEVMODEW)alloca(sizeof(DEVMODEW)+lpa->dmDriverExtra), lpa))
  160. #define TEXTMETRICW2A(lptmw)\
  161.     ((lptmw == NULL) ? NULL : ATLTEXTMETRICW2A((LPTEXTMETRICA)alloca(sizeof(TEXTMETRICA)), lptmw))
  162. #define TEXTMETRICA2W(lptma)\
  163.     ((lptma == NULL) ? NULL : ATLTEXTMETRICA2W((LPTEXTMETRICW)alloca(sizeof(TEXTMETRICW)), lptma))
  164.  
  165. #ifdef OLE2ANSI
  166.     #define DEVMODEOLE DEVMODEA
  167.     #define LPDEVMODEOLE LPDEVMODEA
  168.     #define TEXTMETRICOLE TEXTMETRICA
  169.     #define LPTEXTMETRICOLE LPTEXTMETRICA
  170. #else
  171.     #define DEVMODEOLE DEVMODEW
  172.     #define LPDEVMODEOLE LPDEVMODEW
  173.     #define TEXTMETRICOLE TEXTMETRICW
  174.     #define LPTEXTMETRICOLE LPTEXTMETRICW
  175. #endif
  176.  
  177. #if defined(_UNICODE)
  178. // in these cases the default (TCHAR) is the same as OLECHAR
  179.     inline LPDEVMODEW DEVMODEOLE2T(LPDEVMODEOLE lp) { return lp; }
  180.     inline LPDEVMODEOLE DEVMODET2OLE(LPDEVMODEW lp) { return lp; }
  181.     inline LPTEXTMETRICW TEXTMETRICOLE2T(LPTEXTMETRICOLE lp) { return lp; }
  182.     inline LPTEXTMETRICOLE TEXTMETRICT2OLE(LPTEXTMETRICW lp) { return lp; }
  183. #elif defined(OLE2ANSI)
  184. // in these cases the default (TCHAR) is the same as OLECHAR
  185.     inline LPDEVMODE DEVMODEOLE2T(LPDEVMODEOLE lp) { return lp; }
  186.     inline LPDEVMODEOLE DEVMODET2OLE(LPDEVMODE lp) { return lp; }
  187.     inline LPTEXTMETRIC TEXTMETRICOLE2T(LPTEXTMETRICOLE lp) { return lp; }
  188.     inline LPTEXTMETRICOLE TEXTMETRICT2OLE(LPTEXTMETRIC lp) { return lp; }
  189. #else
  190.     #define DEVMODEOLE2T(lpo) DEVMODEW2A(lpo)
  191.     #define DEVMODET2OLE(lpa) DEVMODEA2W(lpa)
  192.     #define TEXTMETRICOLE2T(lptmw) TEXTMETRICW2A(lptmw)
  193.     #define TEXTMETRICT2OLE(lptma) TEXTMETRICA2W(lptma)
  194. #endif
  195.  
  196. #endif //_WINGDI_
  197.  
  198. #else //!USES_CONVERSION
  199.  
  200. // if USES_CONVERSION already defined (i.e. MFC_VER < 4.21 )
  201. // flip this switch to avoid atlconv.cpp
  202. #define _ATL_NO_CONVERSIONS
  203.  
  204. #endif //!USES_CONVERSION
  205.  
  206. // Define these even if MFC already included
  207. #if defined(_UNICODE)
  208. // in these cases the default (TCHAR) is the same as OLECHAR
  209.     inline LPOLESTR CharNextO(LPCOLESTR lp) {return CharNextW(lp);}
  210. #elif defined(OLE2ANSI)
  211. // in these cases the default (TCHAR) is the same as OLECHAR
  212.     inline LPOLESTR CharNextO(LPCOLESTR lp) {return CharNext(lp);}
  213. #else
  214.     inline LPOLESTR CharNextO(LPCOLESTR lp) {return (LPOLESTR)(lp+1);}
  215. #endif
  216.  
  217. #pragma pack(pop)
  218.  
  219. #endif // __ATLCONV_H__
  220.  
  221. /////////////////////////////////////////////////////////////////////////////
  222.